package org.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.example.erp.domain.*;
import org.example.erp.myInterface.NodeMapper;
import org.example.erp.service.ProWorkorderBomService;
import org.example.erp.service.ProWorkorderService;
import org.example.erp.mapper.ProWorkorderMapper;
import org.example.erp.service.ProductBomService;
import org.example.erp.service.UnitsService;
import org.example.erp.utils.TreeBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
* @author 86180
* @description 针对表【pro_workorder(生产工单表)】的数据库操作Service实现
* @createDate 2024-12-09 16:05:53
*/
@Service
public class ProWorkorderServiceImpl extends ServiceImpl<ProWorkorderMapper, ProWorkorder>
    implements ProWorkorderService{

    @Autowired
    ProductBomService productBomService;

    @Autowired
    UnitsService unitsService;

    @Autowired
    ProWorkorderBomService proWorkorderBomService;

    public Page<ProWorkorder> findPageList(
            String workorderCode,String workorderName,String sourceCode,
            String productCode,String productName,String clientCode,
            String clientName,String workorderType, Date requestDate,
            Page<ProWorkorder> page
    ){
        Page<ProWorkorder> pageList = baseMapper.findPageList(page,workorderCode,workorderName,sourceCode,productCode,productName,clientCode,clientName,workorderType,requestDate);
        TreeBuild<ProWorkorder> treeBuild = new TreeBuild<>();
        // 创建NodeMapper实例
        NodeMapper<ProWorkorder> sysDeptNodeMapper = TreeBuild.createNodeMapper(
                ProWorkorder::getWorkorderId,
                ProWorkorder::getParentId,
                ProWorkorder::getChildren,
                ProWorkorder::setChildren
        );
        List<ProWorkorder> proWorkorders = treeBuild.buildTree(pageList.getRecords(), sysDeptNodeMapper);
        pageList.setRecords(proWorkorders);
        pageList.setTotal(proWorkorders.size());
        return pageList;
    }

    @Transactional
    public boolean add(ProWorkorder proWorkorder) {
        proWorkorder.setCreateTime(new Date());
        boolean save = save(proWorkorder);
        // 查询产品是否有Bom组成
        List<ProductBom> byId = productBomService.findById(proWorkorder.getProductId());
        if (byId != null) {
            for (ProductBom p : byId) {
                QueryWrapper<Units> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Units::getUnitName, p.getBomUnit());
                Units list = unitsService.getOne(queryWrapper);

                ProWorkorderBom proWorkorderBom = new ProWorkorderBom();
                proWorkorderBom.setItemId(p.getBomMaterialId());
                proWorkorderBom.setItemCode(p.getBomMaterialCode());
                proWorkorderBom.setItemName(p.getBomMaterialName());
                proWorkorderBom.setItemSpc(p.getBomMaterialSpec());
                proWorkorderBom.setUnitOfMeasure(list.getUnitName());
                proWorkorderBom.setItemOrProduct(p.getBomMaterial());
                Integer o =proWorkorder.getQuantity() * p.getQuantity();
                proWorkorderBom.setQuantity(o);
                proWorkorderBom.setWorkorderId(proWorkorder.getWorkorderId());
                boolean s = proWorkorderBomService.save(proWorkorderBom);
            }
        }
        return save;
    }

    @Transactional
    public boolean updateA(ProWorkorder proWorkorder) {
        boolean updateById = updateById(proWorkorder);
        // 查询产品是否有Bom组成
        List<ProductBom> byId = productBomService.findById(proWorkorder.getProductId());
        if (byId != null) {
            QueryWrapper<ProWorkorderBom> query = new QueryWrapper<>();
            query.lambda().eq(ProWorkorderBom::getWorkorderId,proWorkorder.getWorkorderId());
            proWorkorderBomService.remove(query);
            for (ProductBom p : byId) {
                QueryWrapper<Units> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Units::getUnitName, p.getBomUnit());
                Units list = unitsService.getOne(queryWrapper);

                ProWorkorderBom proWorkorderBom = new ProWorkorderBom();
                proWorkorderBom.setItemId(p.getBomMaterialId());
                proWorkorderBom.setItemCode(p.getBomMaterialCode());
                proWorkorderBom.setItemName(p.getBomMaterialName());
                proWorkorderBom.setItemSpc(p.getBomMaterialSpec());
                proWorkorderBom.setUnitOfMeasure(list.getUnitName());
                proWorkorderBom.setItemOrProduct(p.getBomMaterial());
                Integer o =proWorkorder.getQuantity() * p.getQuantity();
                proWorkorderBom.setQuantity(o);
                proWorkorderBom.setWorkorderId(proWorkorder.getWorkorderId());
                boolean s = proWorkorderBomService.save(proWorkorderBom);
            }
        }
        return updateById;
    }

    public List<ProWorkorder> find(String workorderCode) {
        return baseMapper.find(workorderCode);
    }

}




